<?php
	function generateFamilyXMLFile() {
		if (isset($_POST["generateXML"])) {
			$n = $_POST["nr_hidden"];
			
			$object_name = "family";
			$i=1;
			do {
				$file_name = $object_name.$i.".xml";
				$i++;
			} while (file_exists($file_name));
			$fh = fopen($file_name, 'w') or die("can't open file");
			
			$line = '<?xml version="1.0" encoding="ISO-8859-1"?>';
			fwrite($fh, $line."\r\n");			
			$line = '<Family count="'.$n.'">';
			fwrite($fh, $line."\r\n");				
			for ($i=1;$i<=$n;$i++) {
				$line = '<Person firstName="'.$_POST["firstName".$i].'" lastName="'.$_POST["lastName".$i].'" age="'.$_POST["age".$i].'" sex="'.$_POST["sex".$i].'" id="'.$i.'"/>';
				fwrite($fh, $line."\r\n");
			}
			$line = '</Family>';
			fwrite($fh, $line);

			fclose($fh);
		}
	}
	
	function generateHouseXMLFile() {
		global $room_types;
//		print_r($_SESSION);
		if (isset($_POST["generateXML"])) {
			$total_rooms_nr = 0;
			foreach ($room_types as $type) {
				$total_rooms_nr += $_SESSION[$type."_no"];
			}
			$object_name = "house";
			$i=1;
			do {
				$file_name = $object_name.$i.".xml";
				$i++;
			} while (file_exists($file_name));
			$fh = fopen($file_name, 'w') or die("can't open file");
			
			$line = '<?xml version="1.0" encoding="ISO-8859-1"?>';
			fwrite($fh, $line."\r\n");			
			$line = '<House count_rooms="'.$total_rooms_nr.'">';
			fwrite($fh, $line."\r\n");
			foreach ($room_types as $type) {
				for ($i=1;$i<=$_SESSION[$type."_no"];$i++) {
					$line = '<Room type="'.$type.'" name="'.$type."_".$i.'"/>';
					fwrite($fh, $line."\r\n");
					for ($j=1;$j<=$_SESSION["nr_sensors"];$j++) {
						if ($_SESSION["sensor_location_".$j]==$type."_".$i) {
							$line = '<Sensor type="'.$_SESSION["sensor_type_".$j].'" name="sensor_'.$j.'" location="'.$_SESSION["sensor_location_".$j].'"/>';
							fwrite($fh, $line."\r\n");
							for ($k=1;$k<=$_SESSION["nr_objects"];$k++) {
								if ($_SESSION["object_sensor_".$k]==$_SESSION["sensor_type_".$j]."_".$type."_".$i) {
									$line = '<Object type="'.$_SESSION["object_type_".$k].'" name="object_'.$k.'" location="'.$_SESSION["sensor_location_".$j].'" sensor="sensor_'.$j.'"/>';
									fwrite($fh, $line."\r\n");
								}
							}
						}
					}
					$line = '</Room>';
					fwrite($fh, $line."\r\n");
				}
			}
			$line = '</House>';
			fwrite($fh, $line);

			fclose($fh);
		}
	}
?>